﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingAffairsManagementSystem.WebControl;

namespace TeachingAffairsManagementSystem.UserControl.RightContent.Admin
{
    public partial class LessonManagement : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["DSLessonCommand"] = null;
                if (Request.QueryString["l_id"] != null) // 通过 QueryString 查询
                {
                    TBSearch.Text = Request.QueryString["l_id"];
                    LBSearchL_ID_Click(null, null);
                }
                else if (Request.QueryString["t_id"] != null)
                {
                    TBSearch.Text = Request.QueryString["t_id"];
                    LBSearchT_ID_Click(null, null);
                }
            }

            else
            {
                // PostBack
                if (Session["DSLessonCommand"] != null)
                {
                    DSLesson.SelectCommand = Session["DSLessonCommand"].ToString();
                }
            }
        }

        protected void LBSearchAll_Click(object sender, EventArgs e)
        {
            DSLesson.SelectCommand = "SELECT * FROM view_lesson ORDER BY l_id DESC";
            Session["DSLessonCommand"] = DSLesson.SelectCommand;
            GVLesson.EditIndex = -1;
            GVLesson.DataBind();
        }

        protected void LBSearchL_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int l_id = Convert.ToInt32(TBSearch.Text);
                DSLesson.SelectCommand = $"SELECT * FROM view_lesson WHERE l_id = {l_id} ORDER BY l_id DESC";
                Session["DSLessonCommand"] = DSLesson.SelectCommand;
                GVLesson.EditIndex = -1;
                GVLesson.DataBind();
            }
            catch
            {
                LBSearchAll_Click(null, null);
            }
        }

        protected void LBSearchT_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int t_id = Convert.ToInt32(TBSearch.Text);
                DSLesson.SelectCommand = $"SELECT * FROM view_lesson WHERE teacher_t_id = {t_id} ORDER BY l_id DESC";
                Session["DSLessonCommand"] = DSLesson.SelectCommand;
                GVLesson.EditIndex = -1;
                GVLesson.DataBind();
            }
            catch
            {
                LBSearchAll_Click(null, null);
            }
        }

        protected void LBSearchName_Click(object sender, EventArgs e)
        {
            string name = TBSearch.Text;
            DSLesson.SelectCommand = $"SELECT * FROM view_lesson WHERE name like '%{name}%' ORDER BY l_id DESC";
            Session["DSLessonCommand"] = DSLesson.SelectCommand;
            GVLesson.EditIndex = -1;
            GVLesson.DataBind();
        }

        protected void GVLesson_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // 判断数据是否为空
            if (e.NewValues["name"] == null)
            {
                Alert.Show("课程名称不能为空", AlertBox.Level.Danger, true);
                e.Cancel = true;
            }
        }

        protected void GVLesson_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            Alert.Show("修改成功", AlertBox.Level.Success, true);
        }

        protected void GVLesson_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "LookUpLessonSelection":
                    int commandIndex = Convert.ToInt32(e.CommandArgument);
                    string l_id = GVLesson.Rows[commandIndex].Cells[0].Text;
                    Utils.OpenWindow($"Main.aspx?p=lsm&l_id={l_id}");
                    break;
            }
        }

        protected void BtnInsert_Click(object sender, EventArgs e)
        {
            int t_id = Convert.ToInt32(DLT_Id.SelectedValue);
            string name = TBName.Text;
            string info = TBInfo.Text;
            if (name == "")
            {
                Alert.Show("课程名称不能为空", AlertBox.Level.Danger, true);
                return;
            }

            DSLesson.InsertParameters["teacher_t_id"].DefaultValue = t_id.ToString();
            DSLesson.InsertParameters["name"].DefaultValue = name;
            DSLesson.InsertParameters["info"].DefaultValue = info;
            DSLesson.Insert();
            GVLesson.DataBind();
            Alert.Show("插入成功", AlertBox.Level.Success, true);
        }

        protected void DLT_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            DLT_Name.SelectedValue = DLT_Id.SelectedValue;
        }

        protected void DLT_Name_SelectedIndexChanged(object sender, EventArgs e)
        {
            DLT_Id.SelectedValue = DLT_Name.SelectedValue;
        }
    }
}